<template>
  <div class="PageTable">
    <MyTable
      :listData="listData"
      :listCount="listCount"
      v-bind="pageTableConfig"
      v-model:page="pageInfo"
    >
      <template #header>
        <div class="header">
          <div class="title">{{ title }}列表</div>
          <el-button
            type="primary"
            size="medium"
            v-if="isCreate"
            @click="handleNewClick"
          >
            新建{{ title }}
          </el-button>
        </div>
      </template>
      <template #status="scope">
        <el-button
          plain
          size="mini"
          :type="scope.row.status ? 'success' : 'danger'"
          >{{ scope.row.status ? '启用' : '禁用' }}</el-button
        >
      </template>
      <template #createAt="scope">
        <span>{{ $filters.formatDate(scope.row.createAt) }}</span>
      </template>
      <template #updateAt="scope">
        <span>{{ $filters.formatDate(scope.row.updateAt) }}</span>
      </template>
      <template #handler="scope">
        <div class="handle-btns">
          <el-button
            size="mini"
            type="primary"
            plain
            v-if="isUpdate"
            @click="handleEditClick(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            plain
            v-if="isDelete"
            @click="handleDeleteClick(scope.row)"
          >
            删除
          </el-button>
        </div>
      </template>
      <template
        v-for="item in otherPropSlots"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </MyTable>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, ref, watch } from 'vue'
import MyTable from '@/base-ui/Table/Table.vue'
import { useStore } from '@/store'
import { usePermission } from '@/hooks/usePermission'
export default defineComponent({
  components: {
    MyTable
  },
  props: {
    title: {
      type: String,
      require: true
    },
    pageTableConfig: {
      type: Object,
      require: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  setup(props, { emit }) {
    const store = useStore()
    const isCreate = usePermission(props.pageName, 'create')
    const isUpdate = usePermission(props.pageName, 'update')
    const isDelete = usePermission(props.pageName, 'delete')
    const isQuery = usePermission(props.pageName, 'query')
    const otherPropSlots = props.pageTableConfig?.propList.filter(
      (item: any) => {
        if (item.slotName === 'status') return false
        if (item.slotName === 'createAt') return false
        if (item.slotName === 'updateAt') return false
        if (item.slotName === 'handler') return false
        return true
      }
    )
    const pageInfo = ref({ currentPage: 1, pageSize: 10 })
    const getPageData = (queryInfo: any = {}) => {
      if (!isQuery) return
      store.dispatch('table/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }
    getPageData()
    watch(pageInfo, () => getPageData())
    const listData = computed(() =>
      store.getters[`table/pageListData`](props.pageName)
    )
    const listCount = computed(() =>
      store.getters[`table/pageListCount`](props.pageName)
    )
    console.log(listData.value)

    // 5.删除/编辑/新建操作
    const handleDeleteClick = (item: any) => {
      console.log(item)
      store.dispatch('table/deletePageDataAction', {
        pageName: props.pageName,
        id: item.id
      })
    }
    const handleNewClick = () => {
      emit('newBtnClick')
    }
    const handleEditClick = (item: any) => {
      console.log(item)

      emit('editBtnClick', item)
    }

    return {
      listData,
      getPageData,
      pageInfo,
      listCount,
      otherPropSlots,
      isCreate,
      isUpdate,
      isDelete,
      handleDeleteClick,
      handleNewClick,
      handleEditClick
    }
  }
})
</script>

<style scoped lang="less">
.PageTable {
  margin-top: 20px;
  // padding: 0 10px;
  .header {
    // width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 10px;
    .title {
      font-size: 20px;
      font-weight: 700;
    }
  }
}
</style>
